package hl.demosample.ui.fragment.welfare

import android.os.Bundle
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.kingja.loadsir.core.LoadService
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.*
import hl.demosample.data.model.bean.WelfareData
import hl.demosample.databinding.FragmentWelfareBinding
import hl.demosample.ui.adapter.WelfareAdapter
import hl.demosample.viewmodel.request.RequestWelfareViewModel
import hl.demosample.viewmodel.state.WelfareModel
import kotlinx.android.synthetic.main.fragment_welfare.*
import kotlinx.android.synthetic.main.include_custom_toolbar.*
import kotlinx.android.synthetic.main.include_smart_list.*
import kotlinx.android.synthetic.main.include_smart_rv.*
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/*
* path: DemoSample 
* Author: hl
* Create: 2021-06-01 17:23
* Changes (from 2021-06-01)
* Desc:
* 7个Flutter重磅开源项目!
* https://www.bilibili.com/read/cv6774472/
*/
class WelfareFragment : BaseFragment<WelfareModel, FragmentWelfareBinding>() {

    private var pageNum = 1

    private val pageSize = 30

    private val welfareAdapter: WelfareAdapter by lazy { WelfareAdapter(arrayListOf()) }

    private var resultsList = ArrayList<WelfareData.ResultsBean>()

    override fun layoutId() = R.layout.fragment_welfare

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    //请求的ViewModel /** */
    private val requestWelfareViewModel: RequestWelfareViewModel by viewModels()

    override fun initView(savedInstanceState: Bundle?) {
        customToolbar.initBack("欢迎来到福利舍") {
            nav().navigateUp()
        }
        //状态页配置
        loadsir = loadServiceInit(listSmartRefresh) {
            //点击重试时触发的操作
            loadsir.showLoading()
            requestWelfareViewModel.getWelfare(pageSize, pageNum)
        }

        recyclerView.init(
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL),
            welfareAdapter
        ).initFloatBtn(floatbtn, true)

        listSmartRefresh.refresh {
            pageNum = 1
            requestWelfareViewModel.getWelfare(pageSize, pageNum)
            listSmartRefresh.finishRefresh()
        }.loadMore {
            requestWelfareViewModel.getWelfare(pageSize, pageNum)
            listSmartRefresh.finishLoadMore()
        }
        welfareAdapter.run {
            setOnItemClickListener { _, view, position ->
                nav().navigateAction(R.id.action_mainfragment_to_photoViewFragment, Bundle().apply {
                    putString("url", welfareAdapter.data[position].url)
                })
            }
        }
    }

    override fun lazyLoadData() {
        loadsir.showLoading()
        requestWelfareViewModel.getWelfare(pageSize, pageNum)
    }

    override fun createObserver() {
        requestWelfareViewModel.welfareData.observe(viewLifecycleOwner, Observer {
            if (pageNum == 1) {
                resultsList.clear()
            }
            resultsList.addAll(it.listData ?: emptyList())
            if (it.listData?.size ?: 0 >= pageSize) {
                pageNum++
            } else {
                listSmartRefresh.setNoMoreData(true)
            }
            if (resultsList.isNotEmpty() && resultsList.size > 0) {
                loadsir.showSuccess()
                welfareAdapter.setList(resultsList)
            } else {
                loadsir.showEmpty()
            }
        })

        appViewModel.run {
            //监听全局的列表动画改编
            appAnimation.observeInFragment(this@WelfareFragment) {
                welfareAdapter.setAdapterAnimation(it)
            }
        }
    }
}